import matplotlib.pyplot as plt
from random_walk import RandomWalk

# 设置默认样式
plt.style.use('classic')
# 解决中文乱码
plt.rcParams["font.sans-serif"]=["SimHei"]
plt.rcParams["font.family"]="sans-serif"
# 解决负号无法显示的问题
plt.rcParams['axes.unicode_minus'] =False


def draw_line(ax, x_vals, y_vals):
    '''绘制线图'''
    ax.plot(x_vals, y_vals, linewidth=3)
    # 设置坐标轴和标题
    ax.set_title('测试线图表', fontsize=24)
    ax.set_xlabel("x数值",fontsize=14)
    ax.set_ylabel("数值",fontsize=14)
    # 刻度信息
    ax.tick_params(labelsize=14)

def draw_scatter(ax, x_vals, y_vals):
    '''绘制点图'''
    ax.scatter(x_vals, y_vals, c=y_vals, cmap=plt.cm.Blues, s=10)
    # 设置坐标轴和标题
    ax.set_title('测试线图表', fontsize=24)
    ax.set_xlabel("x数值",fontsize=14)
    ax.set_ylabel("数值",fontsize=14)
    # 刻度信息
    ax.tick_params(labelsize=14)

def show_squares(ax, max=1000):
    '''显示数据的平方列表'''
    # 构造数据
    input_values = range(1, max+1)
    squares = [x**2 for x in input_values]
    # 绘制图像
    draw_scatter(ax, input_values, squares)
    # 设置坐标系的取值范围
    ax.axis([0, 1100, 0, 1_100_000])
    # 坐标不采用科学计数法
    ax.ticklabel_format(style="plain")

def show_random_walk(ax):
    '''显示随机游走的数据'''
    # 构造游走数据
    rw = RandomWalk(50_000)
    rw.fill_walk()
    # 根据颜色确定游走的先后顺序
    point_numbers = range(rw.num_points)
    # 绘图
    ax.scatter(rw.x_vals, rw.y_vals, c=point_numbers, cmap=plt.cm.Blues, edgecolors='none', s=2)
    ax.set_aspect('equal')
    # 突出起点和重点
    ax.scatter(0, 0, c='green', edgecolors='none', s=20)
    ax.scatter(rw.x_vals[-1], rw.y_vals[-1], c='red', edgecolors='none', s=10)
    # 隐藏坐标轴
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)


# while True:
# 获取绘图
fig, ax = plt.subplots(figsize=(10,6), dpi=128)

# show_squares(ax, 980)   # 绘制平方数
show_random_walk(ax)    # 绘制随机游走数据

plt.show()
# plt.savefig('test_matplot.png', bbox_inches="tight")

    # keep_runing = input("是否继续？(y/n)")
    # if keep_runing == 'n':
    #     break